<?php
class StudentsController extends AppController {

    public $name = 'Students';
    private $current = false;

    public function index(){
        $this->Student->recursive = 0;
        $this->set('students', $this->paginate());
    }

    public function home(){
        $this->Student->recursive = 1;
        $this->set('student', $this->Student->findByName($this->user['name']));
    }

    public function view($id = null){
        if(!$id){
            $this->redirect(array('action'=>'home'));
        }
        $this->set('student', $this->Student->read(null, $id));
    }

    public function add(){
        if(!empty($this->data)){
            $this->Student->create();
            if($this->Student->save($this->data)){
                $this->Session->setFlash(__('The Student has been saved', true));
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash(__('The Student could not be saved. Please, try again.', true));
            }
        }
        $studentMinorities = $this->Student->StudentMinority->find('list');
        $studentProvinces = $this->Student->StudentProvince->find('list');
        $this->set(compact('studentMinorities', 'studentProvinces'));
    }

    public function edit($id = null){
        if(!$id && empty($this->data)){
            $this->Session->setFlash(__('Invalid Student', true));
            $this->redirect(array('action'=>'index'));
        }
        if(!empty($this->data)){
            if($this->Student->save($this->data)){
                $this->Session->setFlash(__('The Student has been saved', true));
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash(__('The Student could not be saved. Please, try again.', true));
            }
        }
        if(empty($this->data)){
            $this->data = $this->Student->read(null, $id);
            $this->set('student', $this->data['Student']);
        }
        $studentMinorities = $this->Student->StudentMinority->find('list');
        $studentProvinces = $this->Student->StudentProvince->find('list');
        $this->set(compact('studentMinorities','studentProvinces'));
    }

    public function delete($id = null){
        if(!$id){
            $this->Response->json(false, __('Invalid id for Student', true));
        }
        if($this->Student->del($id)){
            $this->Response->json(true, __('Student deleted', true));
        }
    }
    /* View application details */
    public function job($student_id=null){
        /* If no user specified, we use the logged in student */
        if(!$student_id && ($this->user['group'] == 'student')){
            $student_id = $this->user['student']['id'];
        }

        /* If no specified or not login, redirect to home page */
        if(!$student_id){
            $this->Session->setFlash(__('You are not logged in or not allowed to do this', true));
            $this->redirect(array('action'=>'home'));
        }
        $this->Student->recursive = 1;
        $this->set('student', $this->Student->read(null, $student_id));
    }

    public function search(){}

}
?>